package cn.westv.scps.web.controller.weather;

import java.util.Map;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;

import cn.westv.scps.constant.ConfigCodeConstant;
import cn.westv.scps.weather.WeatherTools;
import cn.westv.scps.web.controller.base.AbstractController;

/**
 * 获取天气接口
 * @author Administrator
 *
 */
@Controller
@RequestMapping("/weather")
public class WeatherController extends AbstractController<WeatherController>{

	/**
	 * 根据用户所在服务器areaCode获取天气信息
	 * @param areaCode
	 * @return
	 * 		
	 */
	@ResponseBody
	@RequestMapping("/all")
	public Map<String, Object> getWeatherAll(String areaCode){
		JSONObject json = new JSONObject();
		try {
			json = WeatherTools.weatherMap.get(areaCode); //从本地获取缓存好的天气信息
//			System.out.println(WeatherTools.weatherMap);
			if (json != null ) {
				return ajaxReturnMap(OP_STATUS_SUCCESS, "", json);
			}else
				return ajaxReturnMap(OP_STATUS_FAILD, "天气查询失败了！", null);
		} catch (Exception e) {
			logErr(e);
			return ajaxReturnMap(OP_STATUS_FAILD, returnMsg(ConfigCodeConstant.UNKNOWN_ERR), null);
		}
		
	}
	
	/**
	 * 获取简单的天气信息
	 * @param city
	 * @return
	 * 		例：成都 晴 29℃
	 */
	@ResponseBody
	@RequestMapping("/simple")
	public Map<String, Object> getWeatherSimple(String city){
		JSONObject weather = new JSONObject();
		JSONArray list = new JSONArray();
		try {
			weather = WeatherTools.weatherMap.get(city);//从本地获取缓存好的天气信息
			
			if (weather != null) {
//				JSONArray jsonArray = JSON.parseArray(JSON.toJSONString(weather.get("HeWeather5")));//获得json数组
//				//按索引获取其中具体的数据项
//				JSONObject object = JSON.parseObject(JSON.toJSONString(jsonArray.get(0)));
//				JSONObject location = JSON.parseObject(JSON.toJSONString(object.get("basic")));//获得天气基本信息
//				JSONObject now = JSON.parseObject(JSON.toJSONString(object.get("now")));//获得实况天气信息
//				JSONObject cond = JSON.parseObject(JSON.toJSONString(now.get("cond")));//获得天气状况描述信息
//
//				list.add(location.get("city"));
//				list.add(cond.get("txt"));
//				list.add(now.get("tmp") + "℃");
				return ajaxReturnMap(OP_STATUS_SUCCESS, "", weather);
			}
			else
				return ajaxReturnMap(OP_STATUS_FAILD, "天气查询失败了！", null);
		} catch (Exception e) {
			logErr(e);
			return ajaxReturnMap(OP_STATUS_FAILD, returnMsg(ConfigCodeConstant.UNKNOWN_ERR), null);
		}
		
	}


	
	
	
}
